<?php
// +----------------------------------------------------------------------
// | Fengli [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.yanyongwang.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 风黎 < 1280246895@qq.com >
// +----------------------------------------------------------------------


namespace app\api\controller;


use app\common\model\QuestionBank;
use think\Db;
use think\Request;

/**
 * 题库控制器
 * Class Base
 * @package app\admin\controller
 */
class Question extends Base
{
    /**
     * 题库列表
     */
    public function questions()
    {
        $question_bank          = Db::name('question_bank')->field(['qb_name','id'])->select();
        foreach($question_bank as $k=>$value){
            $question_bank[$k]['questions'] = Db::name('questions')->field(['question','A','B','C','D','answer'])->where('qb_id',$value['id'])->select();
        }
        return $this->returnData('200','请求成功',$question_bank);
    }

    /**
     * 用户答题记录
     */
    public function userAnswerLog()
    {
        if(request()->isPost()){
            if(!request()->has('openid'))        return $this->returnData('400','缺少openid');
            if(!request()->has('qb_name'))        return $this->returnData('400','缺少题库名称');
            if(!request()->has('grade'))        return $this->returnData('400','缺少成绩');
            $user            =  Db::name('users')->where(['openid'=>request()->param('openid')])->find();
            if(empty($user))        return $this->returnData('400','没有该用户');
            $log             = [
                'user_id'    => $user['id'],
                'qb_name'    => request()->param('qb_name'),
                'grade'      => request()->param('grade'),
                'create_time'=> time()
            ];
            if(Db::name('answer_record')->insertGetId($log)){
                return $this->returnData('200','添加答题记录成功');
            }else{
                return $this->returnData('400','添加答题记录失败');
            }
        }else{
            return $this->returnData('400','请用POST请求接口');
        }
    }

}